package eerr.llrr.oorr.pte9983.uggtip9324

import android.content.BroadcastReceiver
import android.content.Context
import android.content.Intent
import android.content.IntentFilter
import android.net.wifi.WifiManager
import android.util.Log
import eerr.llrr.oorr.pte9983.b.base_api_keep.BaseApplication
import eerr.llrr.oorr.pte9983.wifi4392.jump98001.RouteJump
import yyee.llee.mmee.pl190.host668.StrongContext
import com.tencent.mmkv.MMKV
import org.json.JSONObject
import java.util.TreeMap

class LeActionRecever : BroadcastReceiver() {

    private var isFirst = true


    companion object {
        private const val SYSTEM_REASON = "reason"
        private const val SYSTEM_HOME_KEY = "homekey"
        private const val SYSTEM_HOME_KEY_LONG = "recentapps"
        private const val SYSTEM_FS_GESTURE = "fs_gesture"


        fun registerScreenAction(context: Context?) {

            try {
                val leActionReceiver = LeActionRecever()
                val filter = IntentFilter()
                filter.addAction(Intent.ACTION_SCREEN_OFF)
                filter.addAction(Intent.ACTION_SCREEN_ON)
                filter.addAction(Intent.ACTION_USER_PRESENT)
                filter.addAction(WifiManager.WIFI_STATE_CHANGED_ACTION)
                filter.addAction(Intent.ACTION_CLOSE_SYSTEM_DIALOGS)
                filter.priority = IntentFilter.SYSTEM_HIGH_PRIORITY
                context?.registerReceiver(leActionReceiver, filter)
            } catch (e: Exception) {
                e.printStackTrace()
            }

        }

    }


    override fun onReceive(context: Context?, intent: Intent?) {

        when (intent?.action) {
            Intent.ACTION_SCREEN_OFF -> {
                if (BaseApplication.isHours() || !yyee.llee.mmee.pl190.host668.SharedPreUtils.getInstance().isFirstEnter) {
                    reqPolicyTime()
                }
            }

            WifiManager.WIFI_STATE_CHANGED_ACTION -> {
                val mWifiState = intent.getIntExtra(WifiManager.EXTRA_WIFI_STATE, 0)
                if (isFirst) {
                    isFirst = false
                    return
                }

                when (mWifiState) {
                    WifiManager.WIFI_STATE_ENABLED -> {
                        RouteJump.safeJump(yyee.llee.mmee.pl190.host668.RouteKey.WIFI_OPEN)
                    }

                    WifiManager.WIFI_STATE_DISABLED -> {
                        RouteJump.safeJump(yyee.llee.mmee.pl190.host668.RouteKey.WIFI_CLOSE)
                    }

                }


            }

            Intent.ACTION_CLOSE_SYSTEM_DIALOGS -> {
                val reason = intent.getStringExtra(SYSTEM_REASON)
                // oppo全面屏，reason错误，homekey和recentapps都被识别成recentapps
//                if (TextUtils.equals(reason, SYSTEM_HOME_KEY)){
                toHomeAction()
//                }
            }

            Intent.ACTION_USER_PRESENT -> {
                Log.e(
                    eerr.llrr.oorr.activitys.Constant3289.TAG,
                    "RouteJump LeActionRecever onReceive: ACTION_USER_PRESENT"
                )
                if (!(yyee.llee.mmee.pl190.host668.RomUtil.isVivo()) || yyee.llee.mmee.pl190.host668.RomUtil.isMiui()) {
                    RouteJump.safeJump(yyee.llee.mmee.pl190.host668.RouteKey.UNLOCK_KEY)
                }
                AdHandlerUtils.startUnlock()
            }


        }

    }


    fun toHomeAction() {
        yyee.llee.mmee.pl190.host668.LiveUtils.getLiveActionObservableForHOME_KEY().post(
            yyee.llee.mmee.pl190.host668.RouteKey.HOME_KEY
        )

    }


    fun reqPolicyTime() {
        yyee.llee.mmee.pl190.host668.UMUtils.getUmOAID()

        val l = MMKV.defaultMMKV().decodeLong("reqplocy:time", 0)

        val currentTime = System.currentTimeMillis() - l

        if (currentTime < 10 * 60 * 1000) {
            return
        }

        yyee.llee.mmee.pl190.host668.DeviceUtils.setUmId(
            yyee.llee.mmee.pl190.host668.UMUtils.getUmId(
                StrongContext.getContext()
            )
        )

        val params = HashMap<String, Any>()
        params.put("from", "update_config")
        yyee.llee.mmee.pl190.host668.DeviceUtils.getFirstInstallTime(params)

        yyee.llee.mmee.pl190.host668.NetManager.getInstance().doPost(
            StrongContext.getApplication(),
            yyee.llee.mmee.pl190.host668.NetMethod.CONFIG,
            params,
            "update_config",
            TreeMap<String, String>(),
            object :
                yyee.llee.mmee.pl190.host668.NetCallback {
                override fun onFailure(code: Int, e: Throwable?) {
                }

                override fun onSuccess(json: JSONObject?) {
                    try {
                        eerr.llrr.oorr.pte9983.b.pl190.host668.ConfigUtils.initConfig(
                            json.toString(),
                            2
                        )
                        MMKV.defaultMMKV().encode("reqplocy:time", System.currentTimeMillis())
                    } catch (e: Exception) {
                        e.printStackTrace()
                    }

                }

            })


    }

}